from CustomGuiItems.MyDockWidget import MyDockWidget;
from InterfaceCmds.InterfaceAPI import InterfaceAPI;
from PyQt4 import QtGui, QtCore, QtNetwork;

class InterfaceNetwork(QtCore.QObject):
    def __init__(self, parent):
        super(InterfaceNetwork, self).__init__();
        self.parent = parent;
        self.mainWin = parent.mainWin;
        self.myDockWidget = MyDockWidget("Interface Network", self.parent);
        self.mainWin.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.myDockWidget);
        self.createDockContent();
        self.myDockWidget.setWidget(self.qWidget);
                
    def onBtnApplyClick(self):
#        self.interfaceApi.rebuildActEdges(1,1);
        qAddress = QtNetwork.QHostAddress(self.txtIp.text());
        nPort = self.spbxPort.value();
        self.qTcpServer.listen(qAddress, nPort);
                
    def onBtnCancelClick(self):
#        self.interfaceApi.clearAll();
#        self.interfaceApi.rebuildActEdges(1,1);
#        self.interfaceApi.executeCmd("clearAll()");
#        self.interfaceApi.executeCmd("rebuildActEdges(1,1)");
        self.qTcpServer.close();
        self.lblStatus.setText("Connection Closed.");
    
    def onClientConnected(self):
        self.lblStatus.setText("Client Connected.");
        self.qSocket = self.qTcpServer.nextPendingConnection();
        self.qSocket.connect(self.qSocket, QtCore.SIGNAL("readyRead()"), self.onReadyRead);
        self.qSocket.connect(self.qSocket, QtCore.SIGNAL("disconnected()"), self.onClientDisconnect);        
        
    def onReadyRead(self):
        self.sData += str(self.qSocket.readAll());
#        print "Data Received: " + str(self.sData); #[0:40] + "...";
#        print "Bytes: " + str(len(self.sData));
        aData = self.sData.split("#");
        for i in range(len(aData)):
            if aData[i]!="" and aData[i].endswith(")"):
                print "Data Received: " + str(aData[i])[0:40] + "...";
                print "Bytes: " + str(len(aData[i]));
                self.lblStatus.setText("Data Received: " + str(aData[i])[0:40] + "\n" + \
                                       "Bytes: " + str(len(aData[i])));
                self.interfaceApi.executeCmd(aData[i]);
        if not aData[len(aData)-1].endswith(")"):
            self.sData = aData[len(aData)-1];
        else:
            self.sData = "";
                
        
    def onClientDisconnect(self):
        self.lblStatus.setText("Client Diconnected.");
        
    def createDockContent(self):
        self.interfaceApi = InterfaceAPI(self.parent);
        self.lblIp = QtGui.QLabel("IP Address:", self.mainWin);
        self.lblPort = QtGui.QLabel("Port Number:", self.mainWin);
        self.lblStatus = QtGui.QLabel("", self.mainWin);        
        self.txtIp = QtGui.QLineEdit("127.0.0.1", self.mainWin);
        self.spbxPort = QtGui.QSpinBox(self.mainWin);
        self.spbxPort.setRange(0, 100000);
        self.spbxPort.setValue(5050);
        self.btnApply = QtGui.QPushButton("Apply", self.mainWin);
        self.btnApply.connect(self.btnApply, QtCore.SIGNAL("clicked()"), self.onBtnApplyClick);
        self.btnCancel = QtGui.QPushButton("Cancel", self.mainWin);
        self.btnCancel.connect(self.btnCancel, QtCore.SIGNAL("clicked()"), self.onBtnCancelClick);
        self.qTcpServer = QtNetwork.QTcpServer(self.mainWin);
        self.qTcpServer.connect(self.qTcpServer, QtCore.SIGNAL("newConnection()"), self.onClientConnected);
        self.onBtnApplyClick();
        self.sData = "";
        
        self.gridLayout = QtGui.QGridLayout();
        self.gridLayout.addWidget(self.lblIp, 0,0, QtCore.Qt.AlignRight);
        self.gridLayout.addWidget(self.txtIp, 0,1, 1,2);
        self.gridLayout.addWidget(self.lblPort, 1,0, QtCore.Qt.AlignRight);
        self.gridLayout.addWidget(self.spbxPort, 1,1, 1,2);
        self.gridLayout.addWidget(self.btnApply, 2,1, QtCore.Qt.AlignRight);
        self.gridLayout.addWidget(self.btnCancel, 2,2);
        self.gridLayout.addWidget(self.lblStatus, 3,0, 1,3, QtCore.Qt.AlignRight | QtCore.Qt.AlignTop);
        self.qWidget = QtGui.QWidget(self.mainWin);
        self.qWidget.setLayout(self.gridLayout);
        
#    def getDataSettings(self):
#        aFlowLayoutData = [];
#        aFlowLayoutData.append(self.spbxPosX.value());
#        aFlowLayoutData.append(self.spbxPosY.value());
#        aFlowLayoutData.append(self.spbxCols.value());
#        aFlowLayoutData.append(self.spbxRows.value());
#        aFlowLayoutData.append(self.spbxSpcX.value());
#        aFlowLayoutData.append(self.spbxSpcY.value());
#        return aFlowLayoutData;
#    
#    def setDataSettings(self, aFlowLayoutData):
#        self.spbxPosX.setValue(aFlowLayoutData[0]);
#        self.spbxPosY.setValue(aFlowLayoutData[1]);
#        self.spbxCols.setValue(aFlowLayoutData[2]);
#        self.spbxRows.setValue(aFlowLayoutData[3]);
#        self.spbxSpcX.setValue(aFlowLayoutData[4]);
#        self.spbxSpcY.setValue(aFlowLayoutData[5]);
        
